package com.vislardo.app.notebook.model;

import cn.hutool.core.util.StrUtil;

import java.util.ArrayList;
import java.util.List;


public class NoteBook {

    private List<NotePage> pages;

    private int totalPage = 0;

    private int currentPage = 1;

    private String noteName;

    private boolean fileChecker;
    private NoteBook() {}

    public NoteBook(List<NotePage> pages,String noteName){
        if(StrUtil.isBlank(noteName)) return;
        this.noteName = noteName;
        if(pages == null) return;
        this.totalPage = pages.size();
        this.pages = pages;

        this.fileChecker = true;

    }

    public NotePage turnPage(int index){

        if(pages == null )return  NotePage.getNewPage(null);

        int total = pages.size();

        if(total == 0||index <=0)return NotePage.getNewPage(null);

        if(index>=total){ this.currentPage=total;return pages.get(total-1).setlast();};

        currentPage = index;

        return pages.get(index-1);
    }

    public void addPage(NotePage page){
        if(page == null || page.isEmpty()) return ;
        pages.add(page);
        this.totalPage = pages.size();
    }


    public static NoteBook startNewNote(String fileName){
        NoteBook note = new NoteBook();
        note.pages = new ArrayList<NotePage>();
        note.noteName = fileName;
        note.totalPage = 1;
        return note;
    }

    public static NoteBook ivalidNote(){
        NoteBook note = new NoteBook();
        note.fileChecker = false;
        return note;
    }

    public NotePage getLastPage(){
        if(pages==null||pages.size()==0) return NotePage.getNewPage(null);
        return pages.get(totalPage-1);
    }

    public int getCurrentPage(){
        return this.currentPage;
    }

    public boolean isLastPage(){
        return this.currentPage==this.totalPage;
    }

}
